//
//  HJTabBarViewController.m
//  Sneakers
//
//  Created by 胡光健 on 2017/9/19.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HJTabBarViewController.h"
#import "HJNavigationViewController.h"
#import "HJMSGVC.h"
#import "HJAddVC.h"
#import "YFJourneyVC.h"
#import "HJMineVC.h"
#import "HJCustomTabBar.h"
#import "LEEAlert.h"
#import "YFCamera.h"
#import "YFImagePicker.h"
#import "TZImageManager.h"
#import "HJPlayerVC.h"
#import "YFChatsVC.h"

@interface HJTabBarViewController ()< UINavigationControllerDelegate>
@property (nonatomic, strong) id popDelegate;
@end

@implementation HJTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
     [UINavigationBar appearance];
    self.delegate = self;
    // 添加所有子控制器
    [self configControl];
    
    self.tabBar.backgroundColor = [UIColor whiteColor];
    //移除顶部线条
//    self.tabBar.backgroundImage = [UIImage new];
//    self.tabBar.shadowImage = [UIImage new];

    int totalUnreadCount = [[RCIMClient sharedRCIMClient] getTotalUnreadCount];
    NSLog(@"当前所有会话的未读消息数为：%d", totalUnreadCount);
    if (totalUnreadCount >=1) {
        [[self.tabBar.items objectAtIndex:2] setBadgeValue:[NSString stringWithFormat:@"%d",totalUnreadCount]];
    }

}

-(void)configControl {
    
    HJNavigationViewController * navHome = [self addChildViewController:[HJPlayerVC new] title:@"首页" image:@"shouye" selecteImage:@"shouye1"];
//    HJNavigationViewController * navJourney = [self addChildViewController:[YFJourneyVC new] title:@"行程" image:@"xingcheng" selecteImage:@"xingcheng1"];
    HJNavigationViewController * navMsg = [self addChildViewController:[YFChatsVC new] title:@"聊天" image:@"xiaoxi" selecteImage:@"xiaoxi1"];
    HJNavigationViewController *navMine = [self addChildViewController:[HJMineVC new] title:@"我的" image:@"wode" selecteImage:@"wode1"];
    self.viewControllers = [NSArray arrayWithObjects:navHome, navMsg, navMine, nil];
}

-(HJNavigationViewController *)addChildViewController:(UIViewController *)childVC title:(NSString *)title image:(NSString * )image selecteImage:(NSString * )selecteImage {
    
    childVC.tabBarItem.image = [[UIImage imageNamed:image]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    childVC.tabBarItem.selectedImage = [[UIImage imageNamed:selecteImage]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    childVC.title = title;
    HJNavigationViewController *nav = [[HJNavigationViewController alloc] initWithRootViewController:childVC];
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSForegroundColorAttributeName] = [UIColor colorWithHex:0xC2C2C2];
    textAttrs[NSFontAttributeName] = [UIFont fontWithName:@"PingFangSC-Regular" size:(SCREEN_WIDTH /375 * 10)];
    
    NSMutableDictionary *selectTextAttrs = [NSMutableDictionary dictionary];
    selectTextAttrs[NSForegroundColorAttributeName] = [UIColor colorWithHex:0x010101];
    selectTextAttrs[NSFontAttributeName] = [UIFont fontWithName:@"PingFangSC-Regular" size: (SCREEN_WIDTH /375 * 10)];
//    nav.delegate = self;
    [nav.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
    [nav.tabBarItem setTitleTextAttributes:selectTextAttrs forState:UIControlStateSelected];
    
    return nav;
}

#pragma mark navVC代理
//- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
//    UIViewController *root = navigationController.viewControllers.firstObject;
//    if (viewController != root) {
//        self.bgImage.hidden = YES;
//        [self.MtabBar removeFromSuperview];
//        [self.bgImage removeFromSuperview];
//        CGRect dockFrame = self.MtabBar.frame;
//        dockFrame.origin.y = root.view.frame.size.height - Height_TabBar;
//        if ([root.view isKindOfClass:[UIScrollView class]]) { // 根控制器的view是能滚动
//            UIScrollView *scrollview = (UIScrollView *)root.view;
//            dockFrame.origin.y += scrollview.contentOffset.y;
//        }
//        self.bgImage.frame = CGRectMake(0, SCREEN_HEIGTH-Height_TabBar-20, SCREEN_WIDTH, Height_TabBar+20);
//        self.MtabBar.frame = dockFrame;
//        [root.view addSubview:self.bgImage];
//        [root.view addSubview:self.MtabBar];
//    }
//}

// 完全展示完调用
//-(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
//    UIViewController *root = navigationController.viewControllers.firstObject;
//    HJNavigationViewController *nav = (HJNavigationViewController *)navigationController;
//    if (viewController == root) {
//        self.bgImage.hidden = NO;
//        navigationController.interactivePopGestureRecognizer.delegate = nav.popDelegate;
//        [_MtabBar removeFromSuperview];
//        [_bgImage removeFromSuperview];
//        _bgImage.frame = CGRectMake(0, SCREEN_HEIGTH-Height_TabBar-20, SCREEN_WIDTH, Height_TabBar+20);
//        _MtabBar.frame = CGRectMake(0, SCREEN_HEIGTH - Height_TabBar, SCREEN_WIDTH, Height_TabBar);
//        [self.view addSubview:_bgImage];
//        [self.view addSubview:_MtabBar];
//    }
//}

@end
